const baseUrl = 'http://192.168.31.199';
const userTokenKey = "satoken";

const ajaxPost = function (url,data,callFn,callError){
    ajax('post',url,data,callFn,callError)
}

const ajaxGet = function (url,params,callFn,callError){
    ajax('get',url,params,callFn,callError)
}


const ajax = function (type,url,params,callFn,callError){
    layui.use(['jquery','context'],function() {
        let $ = layui.jquery;
        const context = layui.context;
        $.ajaxSetup({
            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            headers:{
                "satoken": context.get(userTokenKey)
            }
        });
        $.ajax({
            url: baseUrl + url,
            data: params == null?null:params,
            dataType: 'json',
            // 没有contentType，默认为 multipart/form-data （键值对型数据）
            //contentType: 'application/json',
            type: type,
            success: function (res) {
                if (res.code === 200) {
                    if (isFn(callFn) && callFn(res)) {
                        if (res.msg != null) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 500
                            }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            });
                        } else {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                        }
                    }
                    // else {
                    //     layer.msg(result.msg, {
                    //         icon: 1,
                    //         time: 300
                    //     });
                    // }
                } else {
                    if (res.msg != null) {
                        layer.msg(res.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                    if (isFn(callError)) {
                        callError(res);
                    }
                }
            }
        })
    })
}

const isFn = function (fn) {
    return typeof fn === 'function';
};


const bootEval = function (str){
    return (new Function("return " + str))();
}

const closeMe = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
};


const setCookie = function(name, value,timeout) {
    const exp = new Date();
    exp.setTime(exp.getTime() + parseInt(timeout) * 1000);
    document.cookie = name + "=" + encodeURIComponent(value) + ";path=/;expires=" + exp.toUTCString() + ";";
}

const getCookie = function(cname) {
    const name = cname + "=";
    const ca = document.cookie.split(';');
    for (const element of ca) {
        let c = element;
        while (c.charAt(0) === ' ') c = c.substring(1);
        if (c.indexOf(name) !== -1){
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
const deleteCookie = function(name) {
    document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}

//获取页面之间传递的参数
const getPathParam = function(name) {
    const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    const r = window.location.search.substring(1).match(reg);
    if (r != null) {
        return decodeURI(r[2]);
    }
    return null;
}
function jsonToUrlParam(json) {
    return new URLSearchParams(json).toString();
}

const reverseString = function (str) {
    return str.split('').reverse().join('');
}

const createRandom = function () {
    return new Date().getTime() + '' + Math.floor(Math.random() * 1000000);
};